import { getUser, login } from '../api/modules/user'
import { getSchool } from '../api/modules/school'
import { getStudent } from '../api/modules/student'
import { getTeacher } from '../api/modules/teacher'
import { getClass } from '../api/modules/class'
import { getglbConfig } from '../api/modules/user'
import { getSchoolRole } from '../api/modules/school';
import { getScore } from '../api/modules/score';
// import { groupBy } from '../utils/index.js'
import { textToPy } from './index';

const app = getApp()

export async function init (params) {
    const { 
        jumpHome, // 是否要跳转到 home 页面,默认是 false
     } = params || {}

    const token = uni.getStorageSync('token')
      if (!token) {
        uni.navigateTo({
          url: '/pages/home/home'
        })
        return
      }

      const user = await getUser({page_no_: 1, page_size: 10000})
      app.globalData.userInfo = user.data

      const {
        data: { list: schools }
      } = await getSchool({ page_no: 1, page_size: 10000 })
      const {
        data: { list: classs }
      } = await getClass()
      const {
        data: { list: rolesList }
      } = await getSchoolRole();
      const {
        data: { list: students }
      } = await getStudent({page_no: 1, page_size: 10000}) // 一次性拿 1000条数据
      const {
        data: { list: teachers }
      } = await getTeacher({page_no: 1, page_size: 10000})
      const {
        data: glbConfig
      } = await getglbConfig() // 获取全局配置文件

      const {
        data: { list: scoreList }
      } = await getScore({page_no: 1, page_size: 10000}) // 获取积分

      let static_uri = glbConfig.static_uri;

      const tempSchools = (schools || []).map((school)=>({
        ...school,
        avatar: `${static_uri}${school?.avatar}`,
        classs: classs.filter((class_)=>class_.sid === school?.id),
        students: students.filter((student)=>student.sid === school?.id).map(i => ({...i, avatar: i.avatar ? `${static_uri}${i.avatar}` : null, pname: textToPy(i.name) })),
        teachers: teachers.filter((teacher)=>teacher.sid === school?.id).map(i => ({...i, avatar: i.avatar ? `${static_uri}${i.avatar}` : null, pname: textToPy(i.name) })),
        // roles: rolesList.filter((roles)=>roles.sid === school.id)
      }))


      app.globalData.glb_list.students = students; // 获取全局学生的数据
      app.globalData.glb_list.teachers = teachers; // 获取全局老师的数据
      app.globalData.glb_list.classList = classs; // 获取全局班级的数据
      app.globalData.glb_list.rolesList = rolesList; // 获取用户角色的数据
      app.globalData.glb_list.config = glbConfig; // 获取全局的配置文件
      app.globalData.glb_list.scoreList = scoreList; // 获取全局积分

      app.globalData.glb_list.glbId = schools[0]?.id
      app.globalData.glb_list.glbSchoolName = schools[0]?.name
      app.globalData.schools = tempSchools
      app.globalData.school = tempSchools[0]
      if (jumpHome) {
        uni.navigateTo({
            url: '/pages/home/home'
          })
      }
}

export function loginA (param) {
  login(param).then((res) => {
    if (res.code === 200) {
      uni.setStorage({
        key: 'token',
        data: res.data.token
      })
      getApp().globalData.token = res.data.token
      getApp().globalData.glbCurrentDataMap = res.data
    } 
  })
}

export function notLogin () {
  if (!getApp().globalData.token) {
    uni.navigateTo({
      url: '/pages/mine/login'
    })
  }
}